using System;
using System.Collections.Generic;

namespace TLDRML.Nodes
{
    public class ParentNode : Node, IHasChildren
    {
        public ICollection<Node> ChildNodes { get; private set; }

        public ParentNode()
        {
        }

        public ParentNode(params Node[] children)
        {
            if (children != null)
                ChildNodes = new List<Node>(children);
        }

        public virtual void AddChild(Node child)
        {
            if (child == null)
                throw new ArgumentNullException("child");

            if (ChildNodes == null)
                ChildNodes = new List<Node>();

            ChildNodes.Add(child);
        }

        public void Accept(INodeVisitor visitor)
        {
            visitor.Visit(this);

            visitor.Descend();

            if (ChildNodes != null)
                foreach (ParentNode child in ChildNodes)
                    child.Accept(visitor);

            visitor.Ascend();
        }
    }
}